
#include	"unp.h"

#define NAMESIZE 30
#define RESPONSESIZE 30
#define FILESIZE 2000000

void sig_chld(int signo);

int main(int argc, char **argv)
{
	int					listenfd, connfd, filefd;
	int 				fileCnt, cnt = 0;
	char				fileBuf[MAXLINE];
	char 				fileNameBuf[NAMESIZE];
	pid_t				childpid;
	socklen_t			clilen;
	struct sockaddr_in	cliaddr, servaddr;

	if(argc != 2) {
		printf("usage : <ProgramName> <Port> \n");
		exit(1);
	}

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(atoi(argv[1]));

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	Signal(SIGCHLD, sig_chld);	/* must call waitpid() */

	for ( ; ; ) {
		clilen = sizeof(cliaddr);

		if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) {
			if (errno == EINTR)
				continue;		/* back to for() */
			else
				err_sys("accept error");
		}

		fputs("--------------------------------------------------------------\n", stdout);
		fputs("Client connection\n", stdout);
		printf("connection from %s\n",inet_ntop(AF_INET, &cliaddr.sin_addr, fileBuf, sizeof(fileBuf)));

		if((childpid = Fork()) == 0) {
			Close(listenfd);
			read(connfd, fileNameBuf, NAMESIZE);

			if((filefd = open(fileNameBuf, O_RDONLY)) == -1) {
				printf("fopen error : %s File not found\n", fileNameBuf);
				exit(1);
			}

			fputs("\nFile sending start\n", stdout);

			while((fileCnt = Readline(filefd, fileBuf, MAXLINE)) != 0) {
				writen(connfd, fileBuf, fileCnt);
				cnt = fileCnt;

				printf("%s File Sending\n", fileNameBuf);
				printf("File size \r%d\n", cnt++);//\r%d\n
				sleep(1);
			}

			if((shutdown(connfd, SHUT_WR)) != -1)
				printf("\nClinet IP : %s, %s File Transmission Complete \n", inet_ntop(AF_INET, &cliaddr.sin_addr, fileBuf, sizeof(fileBuf)) , fileNameBuf);

			Close(filefd);
			Close(connfd);

			return 0;
		}
		else
			Close(connfd);
	}
}

void sig_chld(int signo)
{
	pid_t   pid;
	int             stat;

	pid = wait(&stat);
	printf("child %d terminated\n", pid);
	return;
}




/*

#include	"unp.h"

#define NAMESIZE 30
#define RESPONSESIZE 1
#define FILESIZE 1000000

void sig_chld(int signo);

int main(int argc, char **argv)
{
	int					listenfd, connfd, filefd;
	int 				fileCnt, cnt = 0;
	char				fileBuf[MAXLINE];
	char 				fileNameBuf[NAMESIZE];
	char 				responseBuf[RESPONSESIZE];
	pid_t				childpid;
	socklen_t			clilen;
	ssize_t 			n;
	struct sockaddr_in	cliaddr, servaddr;

	if(argc != 2) {
		printf("./FileServ <FIleName.txt>\n");
		exit(1);
	}

	if(argc != 2) {
			printf("usage : <ProgramName> <Port> \n");
			exit(1);
	}

	//strcpy(fileNameBuf, argv[1]);

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(atoi(argv[1]));

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	Signal(SIGCHLD, sig_chld);	 must call waitpid()

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

		puts("Client connection");
		printf("connection from %s, port : %d\n",inet_ntop(AF_INET, &cliaddr.sin_addr, fileBuf, sizeof(fileBuf)), ntohs(cliaddr.sin_port));

		//if ( (childpid = Fork()) == 0) {
		//Close(listenfd);
		read(connfd, fileNameBuf, NAMESIZE);
		//write(connfd, fileNameBuf, NAMESIZE);
		//read(connfd, responseBuf, RESPONSESIZE);

		//printf("debug read ok\n");

		//if(strcmp(responseBuf, "Y") == 0 || strcmp(responseBuf, "y") == 0) {
		//	printf("debug Y read ok\n");

			if((filefd = open(fileNameBuf, O_RDONLY)) == -1) {
				printf("fopen error\n");
				exit(0);
			}
			//printf("debug file open ok\n");

			fputs("File sending start\n", stdout);

			while((fileCnt = read(filefd, fileBuf, MAXLINE)) != 0) {
				write(connfd, fileBuf, fileCnt);
				cnt = fileCnt;
				printf("File Sending \n");
				printf("File size %d\n", cnt++);//\r%d\n
				sleep(1);
			}

			if((shutdown(connfd, SHUT_WR)) != -1)
				printf("Clinet IP : %s, %s File Transmission Complete \n", inet_ntop(AF_INET, &cliaddr.sin_addr, fileBuf, sizeof(fileBuf)) , fileNameBuf);

			Close(connfd);
		//}

		//}
	}
	Close(filefd);

	return 0;
}

void sig_chld(int signo)
{
        pid_t   pid;
        int             stat;

        pid = wait(&stat);
        printf("child %d terminated\n", pid);
        return;
}

 */
